﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLULearningNetworkModel
{
    public partial class OtherNotification
    {
        public static int UpdateReadState(Guid otherNotificationID, Guid accountID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    OtherNotification otherNotification = db.OtherNotifications.SingleOrDefault(fn => fn.OtherNotificationID_u == otherNotificationID);
                    if (otherNotification != null && otherNotification.AccountID_u == accountID)
                    {
                        otherNotification.IsRead_b = true;
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }
                    }
                    return 0;
                }
            }
            catch
            {
                return 101;
            }
        }

        public static int Delete(Guid otherNotificationID, Guid accountID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    OtherNotification otherNotification = db.OtherNotifications.SingleOrDefault(fn => fn.OtherNotificationID_u == otherNotificationID);
                    if (otherNotification != null && otherNotification.AccountID_u == accountID)
                    {
                        db.OtherNotifications.DeleteObject(otherNotification);
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }
                    }
                    return 0;
                }
            }
            catch
            {
                return 101;
            }
        }

        public static List<OtherNotification> GetNewOtherNotification(Guid accountID, int skip = 0, int take = 10)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                   OtherNotification o = new OtherNotification();
                    
                    return db.OtherNotifications.Include("Feed").Include("Feed.Account")
                        .Include("Feed.Account1").Include("Feed.FeedComment.Feed")
                        .Include("Feed.FeedComment.Feed.Account")
                        .Include("Feed.FeedComment.Feed.Account1")
                        .Where(oNotification => oNotification.AccountID_u == accountID && oNotification.IsRead_b == false)
                        .OrderByDescending(oNotification => oNotification.Feed.CreateDate_dt).Skip(skip).Take(take).ToList();
                }   
            }
            catch
            {
                return null;
            }
        }

        public static List<OtherNotification> GetPager(IQueryable<OtherNotification> otherNotifications, int page, int numberOfItemPerPage, ref int totalItems)
        {
            totalItems = otherNotifications.Count();
            if (page < 1)
            {
                page = 1;
            }

            return otherNotifications.OrderByDescending(on => on.Feed.CreateDate_dt).OrderBy(on => on.IsRead_b).Skip((page - 1) * numberOfItemPerPage).Take(numberOfItemPerPage).ToList();
        }

        public static List<OtherNotification> GetAllOtherNotification(Guid accountID, int page, int numberOfItemPerPage, ref int totalItems)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    IQueryable<OtherNotification> query = db.OtherNotifications.
                            Include("Feed").Include("Feed.Account")
                            .Include("Feed.Account1").Include("Feed.FeedComment.Feed")
                            .Include("Feed.FeedComment.Feed.Account")
                            .Include("Feed.FeedComment.Feed.Account1")
                            .Where(fn => fn.AccountID_u == accountID);
                    return GetPager(query, page, numberOfItemPerPage, ref totalItems);
                }
            }
            catch
            {
                return null;
            }
        }

        public static List<OtherNotification> GetLatestOtherNotification(Guid accountID, DateTime time)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    return db.OtherNotifications.Include("Feed").Include("Feed.Account")
                        .Include("Feed.Account1").Include("Feed.FeedComment.Feed")
                        .Include("Feed.FeedComment.Feed.Account")
                        .Include("Feed.FeedComment.Feed.Account1")
                        .OrderBy(on=>on.Feed.CreateDate_dt)
                        .Where(oNotification => oNotification.AccountID_u == accountID && oNotification.IsRead_b == false && oNotification.Feed.CreateDate_dt.CompareTo(time) > 0).ToList();
                }

            }
            catch
            {
                return null;
            }
        }
    }
}
